package com.edu.controller;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.entity.Selection;
import com.edu.entity.User;
import com.edu.service.RoleService;
import com.edu.service.UserService;
import com.edu.utils.FileUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/System/User")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /*方法(1)-用户主页*/
    @RequestMapping("/index")
    public String index(Model model) {
        List<Selection> roleList = roleService.getSelections();
        model.addAttribute("roleList", roleList);
        return "views/user/user_list";
    }

    /*方法(2)-用户列表*/
    @RequestMapping(
            value = "/listPage",
            produces = "application/json;charset=UTF-8"
    )
    @ResponseBody
    public String listPage(HttpServletRequest req) {
        QueryParam QP = new QueryParam(req);
        PageBean PB = userService.listPage(QP);
        return PB.toJson();
    }

    /*方法(3)-用户头像的显示*/
    private String DIR = "D:\\java1\\upload\\user\\";

    @GetMapping("/showHeadImage/{imgName}")
    public void showHeadImage(
            @PathVariable("imgName") String imgName,
            HttpServletResponse resp) {
        try {
            outFile(resp, DIR, imgName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*方法(4)-用户头像的上传*/
    @PostMapping("/uploadHead")
    @ResponseBody
    public R uploadHead(
            @RequestParam("file") MultipartFile mFile,
            @RequestParam("id") Integer id
    ) {
        //1.创建文件上传器[FD]
        FileUploader FD = new FileUploader(mFile, DIR, true);
        //2.执行文件上传.
        userService.updateUserImage(id, FD);
        //3.返回一个ok结果
        return R.ok();
    }

    /*方法(5)-添加用户*/
    @PostMapping("/addUser")
    @ResponseBody
    public R addUser(User user) {
        Integer id = null;
        try {
            id = userService.addUser(user);
            return R.ok().put("userId", id);
        } catch (Exception e) {
            e.printStackTrace();
            //这个方法会检测是否发生账号重复的情况
            //如果重复，返回一个错误的提示信息
            return checkRepeat(e, "'uni_name'", "账号已存在.");
        }
    }

    /*方法(6)-
        更新提交地址:/System/User/updateUser
    */
    @PostMapping("/updateUser")
    @ResponseBody
    public R updateUser(User user) {
        try {
            userService.updateUser(user);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /*方法(7)-*/
    //提交地址:/System/User/deleteUser
    @DeleteMapping("/deleteUser/{id}")
    @ResponseBody
    public R deleteUser(@PathVariable("id") Integer id) {
        try {
            userService.deleteUser(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("用户删除失败:" + e.getMessage());
        }
    }
}
